<template>
  <div class="login">
    {{tipText}}
  </div>
</template>

<script>
import {getCodeImg, oAuthLogin} from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "OAuthLogin",
  data() {
    return {
      tipText: '登录跳转中，请等待'
    };
  },
  created() {
    this.oAuthLogin()
  },
  methods: {
    oAuthLogin () {
      let params = this.getRequest()
      let code = params.code
      if (code === null || code === undefined) {
        let corpId = 'ww1ebf94a46c9b46df'
        let agentId = '1000120'
        let redirectUri = '7.24.8.152:8080/oAuthPage'
        let responseType = 'code'
        let scope = 'snsapi_base'
        let state = 'wx'
        window.location.href = `https://wx.bankalliance.com.cn/connect/oauth2/authorize?appid=${corpId}&redirect_uri=${redirectUri}&response_type=${responseType}&scope=${scope}&agentid=${agentId}&state=${state}#wechat_redirect`
      } else {
        this.$store.dispatch("OAuthLogin", code).then(() => {
          this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
        }).catch(err => {
          this.tipText = err
        });
      }
    },
    getRequest() {
      let url = location.search; //获取url中"?"符后的字串
      let theRequest = new Object();
      if (url.indexOf("?") != -1) {
        let str = url.substr(1);
        let strs = str.split("&");
        for(let i = 0; i < strs.length; i ++) {
          theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);
        }
      }
      return theRequest;
    }

  }
};
</script>
